
import { reqGoodsInfo, reqAddOrUpdateShopCart } from "@/api"
//封装临时游客uuid_token模块，生成一个随机字符串（不能再变）
import { getUUID } from "@/utils/uuid_token";
const state = {
    goodInfo: {},
    //游客临时身份
    uuid_token: getUUID()
}
const mutations = {
    GETGOODINFO(state, goodInfo) {
        state.goodInfo = goodInfo
    }
}
const actions = {
    //获取产品信息的action
    async getGoodInfo({ commit }, skuId) {
        let result = await reqGoodsInfo(skuId)
        if (result.code == 200) {
            commit("GETGOODINFO", result.data)
        }
    },
    //将产品添加到购物车中
    async AddOrUpdateShopCart({ commit }, { skuId, skuNum }) {
        let result = await reqAddOrUpdateShopCart(skuId, skuNum);
        //需要知道请求成功或者失败，如果成功进行跳转，如果失败，需要给用户提示
        if (result.code == 200) {
            return "ok";
        } else {
            return Promise.reject(new Error("faile"));
        }
    }
}
const getters = {
    //state.goodInfo等初始值为空对象，空对象的categoryView属性值为undifined
    categoryView(state) {
        return state.goodInfo.categoryView || {};
    },
    skuInfo(state) {
        return state.goodInfo.skuInfo || {};
    },
    spuSaleAttrList(state) {
        return state.goodInfo.spuSaleAttrList || {};
    }
}
export default {
    state,
    mutations,
    actions,
    getters
}